import {
  HTTP
} from '../../utils/http.js'
let Myhttp = new HTTP()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    questionnaire: [],
    page:1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let aa = wx.getStorageSync('info')
    if (!aa.userInfo) {
      wx.login({
        success: function (res) {
          wx.setStorageSync('code', res.code)
          Myhttp.request({
            url: 'questionnaire/index/getSessionKey',
            method: 'POST',
            data: {
              code: res.code
            },
            success: (res) => {
              wx.setStorageSync('id', res.data.data.unid)
              wx.navigateTo({
                url: '/pages/authorize/index',
              })
            },
          })
        }
      });
    }
  },
  lists_click: function(e) {
    // console.log(e.detail)
  },
  more: function(e) {
    Myhttp.jump({
      url: "../questionnaireList/index",
      success: (res) => {
        // startsWith endsWith 字符串开头 结尾
        // parames.success(res)
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    Myhttp.request({
      url: 'questionnaire/index/index',
      method: 'POST',
      data: {
        page: this.data.page,
        type: '1'
      },
      success: (res) => {
        let datas = res.data.data.list
        let slider = res.data.data.slider
        if (datas[0]){
          for (let i = 0; i < datas.length; i++) {
            datas[i].index = 1
          }
          that.setData({
            'questionnaire': datas,
            imgUrls: slider
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1
    }   
  )
    let that = this
    Myhttp.request({
      url: '/questionnaire/index/index',
      method: 'POST',
      data: {
        page: this.data.page,
        type: '1'
      },
      success: (res) => {
        console.log(res)
        let datas = res.data.data.list
        for (let i = 0; i < datas.length; i++) {
          datas[i].index = 1
          datas[i].src = datas[i].url_domin + datas[i].cover
        }
        that.setData({
          'questionnaire': datas,
        })
        wx.showToast({
          title: '刷新成功'
        })
        //wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let page = this.data.page
    page++
    this.setData({
      page: page
    })
    //console.log(pege)
    let that = this
    Myhttp.request({
      url: '/questionnaire/index/index',
      method: 'POST',
      data: {
        page: this.data.page,
        type: '1'
      },
      success: (res) => {
        //wx.showNavigationBarLoading() //在标题栏中显示加载
        // let imgUrls = []
        let data = res.data.data.list
        let datas = this.data.questionnaire
        // let slider = res.data.data.slider
        for (let i = 0; i < data.length; i++) {
          data[i].index = 1
        }
        data.forEach(val => {
          datas.push(val)
        })

        that.setData({
          'questionnaire': datas,
          // imgUrls: slider
        })
        
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})